<script setup>

import {findCourseAPI} from '@/apis/home'
import {ref,onMounted,watch,computed, onUpdated} from 'vue'

const courList = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const orderBy = ref('create_time')
const order = ref('asc')
const courseName = ref('')

const getCourList = async (page, size) => {
    // console.log(orderBy.value,order.value,courseName.value)
    const res = await findCourseAPI({ pageSize: size, pageNum: page, orderBy: orderBy.value, order:order.value,name:courseName.value })
    courList.value = res.data.list
    console.log(courList.value)
    total.value = res.data.total
}

const getCourListByOrder = (order_by,orderr) => {
  console.log('getCourListByOrder', order)
  orderBy.value = order_by
  order.value = orderr
  getCourList(currentPage.value, pageSize.value)
}



onMounted(() => {
    orderBy.value='like_number'
    order.value='desc'
  getCourList(currentPage.value, pageSize.value);
});

const pageList = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = currentPage.value * pageSize.value;
  
  return courList.value.slice(start, end);
});
const handleCurrentChange = (val) => {
          currentPage.value = val
          console.log(val)
          console.log("handleCurrentChange") 
          getCourList(currentPage.value, pageSize.value)
          
 
        };
onMounted(()=>{
    const el = document.querySelectorAll('.nav-left .el-icon')
    console.log(el)
    el.forEach(item=>{
        item.addEventListener('click',()=>{
             const isActive = item.classList.contains('active')
             el.forEach(icon => icon.classList.remove('active'))
            if (!isActive) {
            item.classList.add('active') // 添加所点击图标的激活状态
      }

        })
    })
})

const searchBtn = ()=>{
    getCourList(currentPage.value, pageSize.value)
}
    function dateFormat(time) {
      var date = new Date(time);
      var year = date.getFullYear();
      
      var month =
        date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
      var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    //   var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    //   var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    //   var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      // 拼接
      return year + "-" + month + "-" + day;
    }

</script>


<template>
    <div class="app-course">
        <div class="container">
        <div class="nav">
        <div class="nav-left">
            <div class="time">
                <span>时间</span>
                <div class="tubiao">
                    <el-icon @click="getCourListByOrder('create_time','asc')"><CaretTop /></el-icon>
                    <el-icon @click="getCourListByOrder('create_time','desc')"><CaretBottom /></el-icon>
                </div>
                
            </div>
            <div class="hot">
                <span>热度</span>
                <div class="tubiao">      
                    <el-icon @click="getCourListByOrder('like_number','asc')"><CaretTop /></el-icon>
                    <el-icon @click="getCourListByOrder('like_number','desc')"><CaretBottom /></el-icon>
                </div>
            </div>
        </div>

        
        <div class="search">
            <input type="text" name="q" v-model="courseName" placeholder="搜索">
             <el-button @click="searchBtn">
                 <el-icon ><Search /></el-icon>
             </el-button>     
        </div>
        </div>
        <div class="list">
            <ul>
             <li v-for="item in courList" :key="item.id">
                 <RouterLink :to="`/courseintro/${item.id}`">
                <img :src=item.img />
                <div class="miaoshu dot">
                <span class="title ">{{ item.name }}</span>
                <div class="bottom">
                    <div class="count">
                        <p>{{item.studiedNumber}} 学习</p>
                        <p>
                        <i class="iconfont icon-zan"></i>
                        {{item.likeNumber}} 
                        </p>
                    </div>
                    <span class="datatime">{{dateFormat(item.createTime)}}</span>
                    <p class="depart">@达利不太圆 </p>
                </div>
                </div>
                 </RouterLink>
           </li> 
            </ul>

        </div>
      
   <div class="example-pagination-block">

    <el-pagination layout="prev, pager, next" :total="total" @current-change="handleCurrentChange"
             :current-page="currentPage"   :page-size="PageSize"/>
  </div>
        </div>
    </div>
</template>

<style scoped>
.container{
    width: 1100px;
    margin: 0 auto;
    
    
}
.app-course{
    background-color: rgb(244,245,247);
}
.time, .hot{
    display: inline-block;
    display: flex;
}
.nav{
    display: flex;
    justify-content: space-between;
    padding: 20px 40px;
}
.nav-left{
    display: flex;
}
.time{
    margin-right: 10px;
}
.tubiao{
    display: flex;
    flex-direction: column;
    justify-content: flex-start ;
}
.tubiao .el-icon:nth-child(2){
    position: relative;
    top:-6px;
}
.nav input{
    width: 120px;
    height: 29px;
    border: none;
    outline: none;
    padding: 4px 6px;
}
 ul{
    display: flex;
    flex-wrap: wrap;
    margin-top: 10px;
   
}

ul li {
  box-shadow: 0px 0px 0px rgba(0,0,0,0.2);
  transition: box-shadow 0.3s;
  border:1px solid #EBEEF5;
}

ul li:hover {
  box-shadow: 3px 5px 10px rgba(0,0,0,0.3);
}
 ul li{
    list-style: none;
    width: 205px;
    height: 240px;
    margin-right: 18px;
      /* border: 1px solid #000; */
    background-color: #fff;
     border-radius: 5px;
     margin-bottom: 30px;
}
ul li:nth-child(5n){
    margin-right: 0;
}
li img{
    width: 100%;
    height: 120px;
     border-radius: 5px;

}
li .count{
    margin-top: 15px;
    display: flex;
    color:#999;
    /* margin-bottom: 5px; */
    font-size: 14px;
   
}
li .datatime {
    color:#999;
    font-size: 14px;
}
li .count p{
    margin-right: 20px;
}
li .miaoshu{
    padding: 0 10px;
}
li .depart{
    font-size: 14px;
    color:#ccc;

}
.dot {
    display: inline-block;
    width: 200px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}


.example-pagination-block .example-demonstration {
  margin-bottom: 16px;
 
}
.example-pagination-block {
  width: 200px;
  height: 200px;
  margin: auto;
}
.el-icon{
    cursor: pointer;
}
.nav-left .active{
    color: red;
}
</style>